package com.yuyou.fn.common.lang;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Combination<T> {

    private List<List<T>> retList ;

    public  List<List<T>> run(T chs[]){
        if(chs.length == 0) return null ;

        retList = new ArrayList<>(100);
        Stack<T> stack = new Stack<T>();
        for(int i = 1; i <= chs.length; i++){
            combine(chs, 0, i, stack);
        }

        return retList ;
    }
    //从字符数组中第begin个字符开始挑选number个字符加入list中
    public void combine(T []chs, int begin, int number, Stack<T> stack){
        if(number == 0){
            retList.add(new ArrayList<T>(stack));
            return ;
        }
        if(begin == chs.length){
            return;
        }
        stack.push(chs[begin]);
        combine(chs, begin + 1, number - 1, stack);
        stack.pop();
        combine(chs, begin + 1, number, stack);
    }

}
